//
//  FavoriteViewController.m
//  VNBooks
//
//  Created by hainguyen on 12/15/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "FavoriteViewController.h"
#import "VNBooksAppDelegate.h"
#import "Chapter.h"
#import "ChapterExtension.h"
#import "ChapterDetailViewController.h"

@implementation FavoriteViewController

@synthesize titleView;
@synthesize titleImageView;
@synthesize favoriteTable;
@synthesize chapterCell;
@synthesize chapterList;

#pragma -
#pragma UItable View Delegate & Data Source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [chapterList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"Cell ID";
    chapterCell = (ChapterCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (chapterCell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"ChapterCell" owner:self options:nil];
    }
    Chapter *chapter = [chapterList objectAtIndex:indexPath.row];
    if ([indexPath row] == 0) {
        chapterCell.cellIndex = first;
    }
    else if ([indexPath row] == ([chapterList count]-1))
    {
        chapterCell.cellIndex = last;
    }
    else{
        chapterCell.cellIndex = middle;
    }
    chapterCell.chapterNameLabel.text = chapter.name;
    chapterCell.excerptLabel.text = chapter.excerpt;
    return [chapterCell autorelease];

    chapterCell.chapterNameLabel.text = chapter.name;
    chapterCell.excerptLabel.text = chapter.excerpt;
    return [chapterCell autorelease];
}

#pragma -

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 61;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChapterDetailViewController *chapterDetailViewController = [[ChapterDetailViewController alloc] 
                                                                init];
    Chapter *chapter = [chapterList objectAtIndex:indexPath.row];
    [mainAppDelegate.mainNavController pushViewController:chapterDetailViewController animated:YES];
    [chapterDetailViewController loadContentWithChapterId:chapter.id];
    [chapterDetailViewController release];
    
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    chapterList = [[NSMutableArray alloc] init];
    [chapterList setArray:[Chapter getChapters]];
    [favoriteTable reloadData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [titleView release];
    [titleImageView release];
    [favoriteTable release];
    [chapterCell release];
    [chapterList release];
    [super dealloc];
}

@end
